package com.banciyuan.bcywebview.biz.goods.address;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.dialog.ConfirmDialog;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.setting.CommonSelectCityBuilder;


public class ReceiverAddressActivity extends BaseActivity implements View.OnClickListener, ActionbarHelper.OnActionCallbacks {
    private EditText etReceiverName;
    private EditText etReceiverPhone;
    private EditText etReceiverDetailAddress;
    private TextView tvReceiverAddress;
    private TextView tv_confirm;
    private String mReceiverName;
    private String mReceiverPhone;
    private String mReceiverAddress;
    private String mReceiverDetailAddress;
    private String mReceiverProvinceCode;
    private String mReceiverCityCode;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_layout);
        initArgs();
        initActionbar();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        uid = UserDataHelper.getInstance(this).getUid();
    }

    @Override
    protected void initProgressbar() {
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.receive_address));
        mActionbarHelper.addItemOneText(getString(R.string.mydialog_comfirm));
        mActionbarHelper.setActionCallbacks(this);
        tv_confirm = (TextView) findViewById(R.id.base_action_bar_home_text_item);
    }

    @Override
    protected void initUi() {
        etReceiverName = (EditText) findViewById(R.id.et_receiver_name);
        etReceiverPhone = (EditText) findViewById(R.id.et_receiver_phone);
        tvReceiverAddress = (TextView) findViewById(R.id.tv_receiver_address);
        etReceiverDetailAddress = (EditText) findViewById(R.id.et_receiver_detailaddrss);
    }

    @Override
    protected void initAction() {
        findViewById(R.id.view_address).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        getReceiverInfo();
        perfomReceiverUiInfo();
    }

    private void perfomReceiverUiInfo() {
        etReceiverName.setText(mReceiverName);
        etReceiverPhone.setText(mReceiverPhone);
        tvReceiverAddress.setText(mReceiverAddress);
        etReceiverDetailAddress.setText(mReceiverDetailAddress);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_address:
                doChangeCity();
                break;
        }
    }

    private void doChangeCity() {
        CommonSelectCityBuilder builder = new CommonSelectCityBuilder(this);
        CommonSelectCityBuilder.ChangeCityListener changeCityListener = new CommonSelectCityBuilder.ChangeCityListener() {
            @Override
            public void onCityChanged(String newCity, String provincecode, String citycode) {
                mReceiverProvinceCode = provincecode;
                mReceiverCityCode = citycode;
                tvReceiverAddress.setText(newCity);
            }
        };
        builder.setChangeCityListener(changeCityListener);
        builder.create().show();
    }

    @Override
    public void onBackClick() {
        showConfirmExitDialog();
    }

    private void showConfirmExitDialog() {
        ConfirmDialog.Builder builder = new ConfirmDialog.Builder(this);
        builder.setDescString(getString(R.string.modify_info_not_saved))
                .setActionString(getString(R.string.mydialog_comfirm))
                .setCancelString(getString(R.string.mydialog_cancel))
                .setActionClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).create().show();
    }

    @Override
    public void onBackPressed() {
        showConfirmExitDialog();
    }

    @Override
    public void onActionClick(int id) {
        switch (id) {
            case R.id.base_action_bar_home_text_item:
                checkValidity();
                break;
        }
    }

    private void checkValidity() {
        if (checkReceiverName() && checkReceiverPhone() && checkReceiverAddress()) {
            saveReceiverInfo();
            MyToast.show(this, getString(R.string.save_success));
            finish();
        }
    }

    private void saveReceiverInfo() {
        ReceiverAddressHelper.putReceiverName(this, uid, mReceiverName);
        ReceiverAddressHelper.putReceiverPhone(this, uid, mReceiverPhone);
        ReceiverAddressHelper.putReceiverAddress(this, uid, mReceiverAddress);
        ReceiverAddressHelper.putReceiverDetailAddress(this, uid, mReceiverDetailAddress);
        ReceiverAddressHelper.putReceiverProvinceCode(this, uid, mReceiverProvinceCode);
        ReceiverAddressHelper.putReceiverCityCode(this, uid, mReceiverCityCode);
    }

    private void getReceiverInfo() {
        mReceiverName = ReceiverAddressHelper.getReceiverName(this, uid);
        mReceiverPhone = ReceiverAddressHelper.getReceiverPhone(this, uid);
        mReceiverAddress = ReceiverAddressHelper.getReceiverAddress(this, uid);
        mReceiverDetailAddress = ReceiverAddressHelper.getReceiverDetailAddress(this, uid);
        mReceiverProvinceCode = ReceiverAddressHelper.getReceiverProvinceCode(this, uid);
        mReceiverCityCode = ReceiverAddressHelper.getReceiverCityCode(this, uid);
    }

    private boolean checkReceiverAddress() {
        mReceiverAddress = tvReceiverAddress.getText().toString().trim();
        mReceiverDetailAddress = etReceiverDetailAddress.getText().toString().trim();
        if (TextUtils.isEmpty(mReceiverAddress)) {
            MyToast.show(this, getString(R.string.please_input_receiver_address));
            return false;
        }
        if (TextUtils.isEmpty(mReceiverDetailAddress)) {
            MyToast.show(this, getString(R.string.please_input_receiver_detailaddress));
            return false;
        }
        return true;
    }

    private boolean checkReceiverPhone() {
        mReceiverPhone = etReceiverPhone.getText().toString().trim();
        if (TextUtils.isEmpty(mReceiverPhone)) {
            MyToast.show(this, getString(R.string.please_input_receiver_phone));
            return false;
        }
        boolean matches = mReceiverPhone.matches("^\\d{11}$");
        if (!matches) {
            MyToast.show(this, getString(R.string.please_input_correct_receiver_phone));
            return false;
        }
        return true;
    }

    private boolean checkReceiverName() {
        mReceiverName = etReceiverName.getText().toString().trim();
        if (TextUtils.isEmpty(mReceiverName)) {
            MyToast.show(this, getString(R.string.please_input_receiver_name));
            return false;
        }
        return true;
    }
}
